package newFeature;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author LiuYan
 * @date 2023/4/20
 * 变量放到try()里面
 */
public class TryOptimization {
    public static void main(String[] args) throws IOException {
        // Java8
        try (InputStream inputStream8 = Files.newInputStream(Paths.get(""))) {
            for (int i = 0; i < 100; i++) {
                System.out.println((char) inputStream8.read());
            }
        }

        // Java9
        InputStream inputStream9 = Files.newInputStream(Paths.get(""));
        try (inputStream9) {
            for (int i = 0; i < 100; i++) {
                System.out.println((char) inputStream9.read());
            }
        }
    }

}
